// pages/smallTeamEdit/smallTeamEdit.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    grouping_id:'',//小分队的id
    groupingName: '',//分队名字
    subteam_list: {},//小分队信息
    subteam_leader: {},//小分队发起人
    subteam_users: [],//小分队人数
    odd_teamid: '',//单个球队id
    httpPath: app.globalData.imagePath,
    teamIndex:0,
    teamId: [],//球队id数组
    teamName: [],//球队名字
    teamLogo: [],//球队logo
    team_member:[],//球队队员信息
    checkboxUserid: [],//被选中的队员的id
    session_key:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var _key = wx.getStorageSync("token");
    that.setData({
      grouping_id: options.fenduiID,
      session_key: _key
    })
    //小分队信息
    wx.request({
      url: app.globalData.httpsPath + '/subteam/show',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        subteam_id: options.fenduiID
      },
      //请求后台数据成功  
      success: function (res) {
        var fendui_arr = res.data.data;
        var fendui_faqiren = res.data.data.leader;
        var fendui_duiyuan = res.data.data.users;
        var checkboxId = fendui_arr.user_ids.split(",");

        that.setData({
          subteam_list: fendui_arr,
          groupingName: fendui_arr.name,
          subteam_leader: fendui_faqiren,
          subteam_users: fendui_duiyuan,
          odd_teamid: fendui_arr.pid,
          checkboxUserid: checkboxId
        })
        
        //获取球队信息
        wx.request({
          url: app.globalData.httpsPath + '/user/team',
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          data: {
            token: _key
          },
          //请求后台数据成功  
          success: function (res) {
            var teamidArry = [];
            var teamArry = [];
            var teamLogo = [];
            var _index = 0;
            var _createTeam = res.data.data.data;
            var _pid = that.data.subteam_list.pid;

            for (var i = 0; i < _createTeam.length; i++) {
              teamidArry.push(_createTeam[i].team_id);
              teamArry.push(_createTeam[i].team.name);
              teamLogo.push(_createTeam[i].team.logo);
              if (_createTeam[i].team_id == _pid) {
                that.setData({
                  teamIndex:i
                })
              }
            }
            that.setData({
              teamId: teamidArry,
              teamName: teamArry,
              teamLogo: teamLogo
            })
          }
        })
        //球队队员
        wx.request({
          url: app.globalData.httpsPath + '/team/users',
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          data: {
            token: _key,
            team_id: that.data.subteam_list.pid
          },
          //请求后台数据成功  
          success: function (res) {
            var team_member = res.data.data;
            for (var i in team_member) {
              for (var j in fendui_duiyuan){
                if (fendui_duiyuan[j].id == team_member[i].id){
                  team_member[i]['checked'] = 'true';
                }
              }
            }
            that.setData({
              team_member: team_member
            })
          }
        })
      }
    })
    
  },
  //获取分队名
  groupingInput: function (e) {
    this.setData({
      groupingName: e.detail.value
    })
  },
  //切换球队信息
  bindTeamChange: function (e) {
    var that = this;
    that.setData({
      teamIndex: e.detail.value
    })
    var teamId = that.data.teamId[e.detail.value];
    var _key = that.data.session_key;
    wx.showLoading({
      title: '数据加载中'
    })
    //球队队员
    wx.request({
      url: app.globalData.httpsPath + '/team/users',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        team_id: teamId
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var team_member = res.data.data;
        that.setData({
          team_member: team_member
        })
      }
    })
  },
  //复选框的事件
  checkboxChange: function (e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      checkboxUserid: e.detail.value
    })
  },
  //确定修改
  updateGrouping:function(){
    var that = this;
    var _id = that.data.grouping_id;
    var _key = wx.getStorageSync("token");
    var _teamId = that.data.teamId[that.data.teamIndex];
    var _teamLogo = that.data.teamLogo[that.data.teamIndex];
    var _groupName = that.data.groupingName;
    var _checkboxId = that.data.checkboxUserid;
    console.info(_checkboxId)
    var _checkboxIdstr;

    if (_groupName == '') {
      wx.showToast({
        icon: "none",
        title: '请输小分队名称'
      })
      return false;
    }
    if (_checkboxId.length == 0) {
      wx.showToast({
        icon: "none",
        title: '请选择分队成员'
      })
      return false;
    } else {
      _checkboxIdstr = _checkboxId.join(",");
    }
    wx.request({
      url: app.globalData.httpsPath + '/subteam/update',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        pid: _teamId,
        name: _groupName,
        user_ids: _checkboxIdstr,
        id: _id,
        logo: _teamLogo
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res)
        wx.showToast({
          title: res.data.msg,
          icon: 'succes',
          duration: 2000,
          mask: true
        })
        setTimeout(function () {
          wx.redirectTo({
            url: '../smallTeam/smallTeam'
          })
        }, 2000)
      }
    })
  },
})